package cn.gok.server.service.impl;

import cn.gok.common.dao.backstage.ArticleMapper;
import cn.gok.common.entity.backstage.Article;
import cn.gok.server.service.ArticleService;
import cn.gok.utils.DateUtil;
import cn.gok.utils.UserContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: DxlinY
 * @apiNote: 商城资讯相关服务实现类
 * @date: 2020/9/27
 * @time: 11:05
 */
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageInfo pageInfo(Object target) {
        QueryWrapper <Article> wrapper = new QueryWrapper <>();
        wrapper.setEntity((Article) target);
        PageInfo <Article> pageInfo = new PageInfo <>(articleMapper.selectList(wrapper));
        log.info("资讯分页查询成功{}", pageInfo);
        return pageInfo;
    }

    @Override
    public int addGArticle(Article article) {
        article.setUpdateTime(DateUtil.nowDate());
        article.setIsEnabled(false);
        article.setUpdateBy(UserContext.user().getUserName());
        article.setReviewStatus(0);
        article.setClickCount(0);
        article.setCreateBy(UserContext.user().getUserName());
        log.info("添加一条资讯{}", article);
        return articleMapper.insert(article);
    }

    @Override
    public int delByIds(List <Long> ids) {
        log.info("批量删除编号{}", ids);
        return articleMapper.deleteBatchIds(ids);
    }

    @Override
    public Article getById(long id) {
        log.info("获取编号为{}的资讯信息", id);
        return articleMapper.selectById(id);
    }

    @Override
    public int updateById(Article article) {
        log.info("资讯更新{}", article);
        return articleMapper.updateById(article);
    }

    @Override
    public int updateIsEnableById(long articleId, boolean isEnable) {
        log.info("修改{}发布状态为{}", articleId, isEnable);
        return articleMapper.updateIsEnableById(articleId, isEnable);
    }
}